/* $Id: JTradeUtil.java,v 1.10 2007/09/07 06:51:21 gigabass Exp $
*
* Copyright (c) Guido Ludwig. All rights reserved.
* Use is subject to license terms.
*/
package de.guidoludwig.jtm.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.URL;
import java.util.logging.Logger;

import javax.swing.ImageIcon;

import de.guidoludwig.af.ErrorMessage;



/**
 * Utility for JTrade specific operations
 * 
 * @author <a href="mailto:jtrade@gigabss.de">Guido Ludwig</a>
 * @version $Revision: 1.10 $
 */
public class JTMUtil {

    /**
     * Loads an Icon with the given name.
     * The name represents an image source located in the
     * resources/images directory.
     * The name must include the image extension, eg. 'image.gif'
     * <br/>
     * If the image is not found, simply a null Icon is returned !
     * <br/>
     * Possible Exceptions thrown are eaten !
     * If an exception occurs, a null Icon is returned ! 
     * @param name
     * @return the loaded icon or null if the icon could not be loaded
     */
    public static ImageIcon loadIcon(String name) {
        URL url = ClassLoader.getSystemResource("resources/images/" + name); //$NON-NLS-1$
        if (url == null) {
        	Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).warning("icon " + name + "not found"); //$NON-NLS-1$ //$NON-NLS-2$
            return null;
        }
        return new ImageIcon(url);
    }    
    
	public static String loadResource(String resource) throws IOException {
		BufferedReader reader = loadResourceFile(resource);
		if (reader == null) {
			return null;
		}
		StringBuffer buffer = new StringBuffer();
		String line = null;
		while ((line = reader.readLine()) != null) {
			buffer.append(line + "\n");
		}
		return buffer.toString();
	}
    
	public static BufferedReader loadResourceFile(String resource)
			throws IOException {
		URL url = ClassLoader.getSystemResource("src/resources/" + resource);
		if (url == null) {
			ErrorMessage.handle(new NullPointerException("URL for src/resources/" + resource + " not found"));
			return null;
		} else {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					url.openStream()));
			return reader;
		}
	}

    
    public static String loadFile(String filename) {
        StringBuffer buffer = new StringBuffer();
        RandomAccessFile raf;
		try {
			raf = new RandomAccessFile(filename, "r");
		} catch (FileNotFoundException e) {
			ErrorMessage.handle("File not found", e);
			return null;
		}
		String line = null;
        try {
			while ((line = raf.readLine()) != null) {
			    buffer.append(line + "\n");
			}
		} catch (IOException e) {
			ErrorMessage.handle( e);
			return null;
		}
        return buffer.toString();
        
    }         
    
}
